<?php

namespace app\common\model;

use app\admin\model\jeek\shop\SkuGoods;
use think\Model;


class Dynamic extends Model
{


    // 表名
    protected $name = 'dynamic';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [

    ];


    public function skuGoods()
    {
        return $this->belongsTo(SkuGoods::class, 'goods_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }


    /**
     * Account:我的动态分页
     * User: Ixiangang
     * Date: 2025/1/24
     * Time: 16:05
     */
    public function getListPage($where = [], $user_id = 0)
    {
        $row = $this->getTermWhere($where);
        $field = $this->getField();
        $order = ['d.id' => 'desc'];
        $list = $row->alias('d')
            ->join('jeek_shop_sku_goods g', 'd.goods_id = g.id', 'left')
            ->join('jeek_level l', 'g.jeek_level_id = l.id', 'left')
            ->field($field)
            ->order($order)
            ->paginate()
            ->each(function ($item) use (&$extend, &$result, $user_id) {
                if ($item->images) {
                    $item->images = explode(',', $item->images);
                }
                # 查询用户当前用户是否已点赞
                if ($user_id != 0) {
                    $dynamiclog = Dynamiclog::where(['user_id' => $user_id, 'dynamic_id' => $item->id, 'type' => '1'])->find();
                    $item->user_give = !empty($dynamiclog) ? 1 : 0;  //1=已点赞0=未点赞
                }
                return $item;
            });
        return $list;
    }


    /**
     * todo暂无使用
     * Account:我的动态不带分页
     * User: Ixiangang
     * Date: 2025/1/24
     * Time: 16:05
     */
    public function getList($where = [], $limit = 5)
    {
        $row = $this->getTermWhere($where);
        $field = $this->getField();
        $order = ['d.id' => 'desc'];
        $list = $row->alias('d')
            ->join('jeek_shop_sku_goods g', 'd.goods_id = g.id', 'left')
            ->join('jeek_level l', 'g.jeek_level_id = l.id', 'left')
            ->field($field)
            ->order($order)
            ->limit($limit)
            ->select();
        array_map(function ($item) {
            if ($item->images) {
                $item->images = explode(',', $item->images);
            }
            return $item;
        }, $list);
        return $list;
    }


    /**
     * Account:动态详情
     * User: Ixiangang
     * Date: 2025/2/6
     * Time: 14:45
     */
    public function getInfo($where, $user_id = 0)
    {
        $list = self::getList($where);
        $info = $list[0] ?? [];
        if (!$info) return false;
        # 评价内容
        $comm = Dynamiclog::alias('dl')
            ->join('user u', 'dl.user_id = u.id', 'left')
            ->field('dl.id,dl.user_id,dl.content,dl.createtime,dl.type,u.nickname,u.avatar')
            ->where(['dl.dynamic_id' => $info['id'], 'dl.type' => '2'])
            ->select();
        # 查询用户当前用户是否已点赞
        if ($user_id != 0) {
            $dynamiclog = Dynamiclog::where(['user_id' => $user_id, 'dynamic_id' => $info->id, 'type' => '1'])->find();
            $info->user_give = !empty($dynamiclog) ? 1 : 0;  //1=已点赞0=未点赞
        }
        return ['info' => $info, 'comm' => $comm];
    }


    public function getField()
    {
        return "d.id,d.content,d.images,d.goods_id,d.user_id,d.goods_id,d.give_num,d.comm_num,d.createtime,
        g.name as goods_name,g.image as goods_image,g.jeek_level_id,
        l.name as level_name";
    }

    /**
     * Account:列表查询条件
     * User: Ixiangang
     * Date: 2025/1/21
     * Time: 17:23
     */
    public function getTermWhere($where)
    {
        return self::where(function ($query) use ($where) {
            # 通用登录ID查询我的动态
            if (isset($where['user_id']) && $where['user_id']) {
                $query->where('d.user_id', $where['user_id']);
            }
            # 通过咨询师查询我的动态
            if (isset($where['goods_id']) && $where['goods_id']) {
                $query->where('d.goods_id', $where['goods_id']);
            }
            if (isset($where['dynamic_id']) && $where['dynamic_id']) {
                $query->where('d.id', $where['dynamic_id']);
            }
        });
    }


    /**
     * Account:点赞数 、增加 减少
     * User: Ixiangang
     * Date: 2025/2/6
     * Time: 11:25
     */
    public static function getUpdateGive($dynamic_id, $type = -1, $num = 1)
    {
        if (!$dynamic_id && !$type) return false;
        $info = self::get($dynamic_id);
        if (!$info) return false;
        # 减少点赞数据
        if ($type == -1) {
            if ($info->give_num == 0) return false;
            $info->give_num -= $num;
        }
        # 增加点赞数
        if ($type == 1) {
            $info->give_num += $num;
        }
        $info->save();
        return true;
    }


    /**
     * Account:动态评价次数增加
     * User: Ixiangang
     * Date: 2025/2/6
     * Time: 14:14
     */
    public static function getUpdateComm($dynamic_id, $num)
    {
        if (!$dynamic_id) return false;
        $info = self::get($dynamic_id);
        if (!$info) return false;
        $info->comm_num += $num;
        $info->save();
        return true;
    }

}
